//
//  HOTHttpClient.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-19.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTHttpClient.h"
#import "ZZZHttpRequestClient.h"
#import "HOTConstentDefine.h"


#define REQUEST_POST_METHOD @"POST"
#define REQUEST_GET_METHOD @"GET"

@interface HOTHttpClient () {
    
    //ZZZHttpRequestClient *_zzzClient;
}
@property(nonatomic,strong)ZZZHttpRequestClient *zzzClient;
@end

@implementation HOTHttpClient

+(instancetype)client {
    
    static dispatch_once_t onceToken;
    static HOTHttpClient *client;
    dispatch_once(&onceToken, ^{
        client = [[[self class] alloc] init];
        client.zzzClient = [ZZZHttpRequestClient client];
    });
    
    return client;
}

-(void)requestWithURL:(NSString *)urlString
           Parameters:(id)parameters
              success:(void (^)(id))success
              failure:(void (^)(NSError *error))failure {
    
    NSString *url = [self getRequestURL:urlString];
    
//    NSLog(@"=====请求URL：%@",url);
    NSLog(@"=====请求parameters：%@",parameters);
    
    [self.zzzClient POST:url parameters:parameters
                 success:^(AFHTTPRequestOperation *task, id responseObject) {
        
                     if (success) {
                         success(responseObject);
                     }
        
    } failure:^(AFHTTPRequestOperation *task, NSError *error) {
        
        if (failure) {
            //NSString *errMsg = [self parseError:error];
//            NSLog(@"=====服务器请求出错：%@",error);
            failure(error);
        }
    }];
}

-(void)requestGETWithURL:(NSString *)urlString
           Parameters:(id)parameters
              success:(void (^)(id))success
              failure:(void (^)(NSError *error))failure {
    
    //NSString *url = [self getRequestURL:urlString];
    
//    NSLog(@"=====请求URL：%@",urlString);
//    NSLog(@"=====请求parameters：%@",parameters);
    
    [self.zzzClient GET:urlString parameters:parameters
                 success:^(AFHTTPRequestOperation *task, id responseObject) {
                     
                     if (success) {
                         success(responseObject);
                     }
                     
                 } failure:^(AFHTTPRequestOperation *task, NSError *error) {
                     
                     if (failure) {
                         //NSString *errMsg = [self parseError:error];
//                         NSLog(@"=====服务器请求出错：%@",error);
                         failure(error);
                     }
                 }];
}

-(void)uploadDataWithURL:(NSString *)urlString
              uploadData:(NSData *)uploadData
              uploadName:(NSString *)uploadName
                fileName:(NSString *)fileName
              Parameters:(id)parameters
                mimeType:(NSString *)mimeType
                 success:(void (^)(id))success
                 failure:(void (^)(NSError *error))failure {
    
    //NSString *url = [self getRequestURL:urlString];
    NSString *url = [self getRequestURL:urlString];
//    NSLog(@"=====请求URL：%@",urlString);
//    NSLog(@"=====请求parameters：%@",parameters);

    [self.zzzClient POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        if (uploadData) {

            [formData appendPartWithFileData:uploadData name:uploadName fileName:fileName mimeType:mimeType];
            
        }
    } success:^(AFHTTPRequestOperation *task, id responseObject) {
        if (success) {
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *task, NSError *error) {
        
        if (failure) {
            //NSString *errMsg = [self parseError:error];
//            NSLog(@"=====服务器请求出错：%@",error);
            failure(error);
        }
    }];
}

-(NSString *)getRequestURL:(NSString *)subURLString {
    
    NSString *reqUrl = [NSString stringWithFormat:@"%@%@", HOT_URL_HOST, subURLString];
    
    return reqUrl;
}

/* 迁移到baseViewController里 做check
-(NSString *)parseError:(NSError *)error {
    
    NSString *errMsg = error.domain;
    switch (error.code) {
        case NSURLErrorUnknown:
            errMsg = REQ_ERROR_UNKNOW;
            break;
            case NSURLErrorCancelled:
            errMsg = REQ_ERROR_CANCEL;
            break;
            case NSURLErrorBadURL:
            errMsg = REQ_ERROR_BAD_URL;
            break;
            case NSURLErrorTimedOut:
            errMsg = REQ_ERROR_TIMEOUT;
            break;
            case NSURLErrorUnsupportedURL:
            errMsg = REQ_ERROR_UNSUPPORT;
            break;
            case NSURLErrorCannotFindHost:
            errMsg = REQ_ERROR_CONT_FIND_HOST;
            break;
            case NSURLErrorCannotConnectToHost:
            errMsg = REQ_ERROR_CONT_CONNECT_HOST;
            break;
            case NSURLErrorNetworkConnectionLost:
            errMsg = REQ_ERROR_UNKNOW;
            break;
            case NSURLErrorDNSLookupFailed:
            errMsg = REQ_ERROR_DNS;
            break;
            case NSURLErrorHTTPTooManyRedirects:
            break;
            case NSURLErrorResourceUnavailable:
            errMsg = REQ_ERROR_RESOURCE_UNAVAILABLE;
            break;
            case NSURLErrorNotConnectedToInternet:
            errMsg = REQ_ERROR_CONNECT_INTERNET;
            break;
            case NSURLErrorRedirectToNonExistentLocation:
            break;
            case NSURLErrorBadServerResponse:
            break;
            case NSURLErrorUserCancelledAuthentication:
            break;
            case NSURLErrorUserAuthenticationRequired:
            break;
            case NSURLErrorZeroByteResource:
            break;
            case NSURLErrorCannotDecodeRawData:
            break;
            case NSURLErrorCannotDecodeContentData:
            break;
            case NSURLErrorCannotParseResponse:
            break;
            case NSURLErrorFileDoesNotExist:
            break;
            case NSURLErrorFileIsDirectory:
            break;
            case NSURLErrorNoPermissionsToReadFile:
            break;
            case NSURLErrorDataLengthExceedsMaximum:
            break;
        default:
            errMsg = REQ_ERROR_UNKNOW;
            break;
    }
    
    return errMsg;
}
*/
@end
